﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.TownshipEnterprise.Model;
using Yipusoft.TownshipEnterprise.BLL;
using Yipusoft.Common.BLL;

public partial class TownshipEnterprise_SoldierView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //BindArichives();
            TownSoldiersInfo soldierInfo = TownSoldiers.GetTownSoldiers(new Guid(Request["ID"].ToString()));
            bool enableViewed = false;
            //string AccountDepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            //if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "203001"))
            //{
            //    enableViewed = true;
            //}
            //else
            //{
            //    if ((Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "203006")))
            //        enableViewed = AccountDepartmentID.Substring(0, 4) == soldierInfo.DepartmentID.Substring(0, 4);
            //    else
            //        enableViewed = AccountDepartmentID == soldierInfo.DepartmentID;
            //}
            enableViewed = true;
            if (enableViewed)
            {
                //lblTitle.Text = "兵员详细信息";
                SoldierName.Text = soldierInfo.SoldierName;
                CardID.Text = soldierInfo.CardID;
                Sex.Text = soldierInfo.Sex;
                imgPhoto.ImageUrl = soldierInfo.Picture == "" ? "~/Images/NoPhoto.png" : soldierInfo.Picture;
                NativeName.Text = CodeItem.GetCodeItem(soldierInfo.Native).ItemName;
                NationName.Text = CodeItem.GetCodeItem(soldierInfo.Nation).ItemName;
                BackgroundName.Text = CodeItem.GetCodeItem(soldierInfo.Background).ItemName;
                Marriage.Text = soldierInfo.Marriage;
                CultureName.Text = CodeItem.GetCodeItem(soldierInfo.Culture).ItemName;
                ReligionName.Text = CodeItem.GetCodeItem(soldierInfo.Religion).ItemName;
                //DepartmentName.Text = soldierInfo.Department;
                Birthday.Text = soldierInfo.Birth.Year == 1 ? "--" : soldierInfo.Birth.Year == 9999 ? "--" : soldierInfo.Birth.ToString("yyyy-MM-dd");
                JoinDate.Text = soldierInfo.JoinTime.Year == 1 ? "--" : soldierInfo.JoinTime.Year == 9999 ? "--" : soldierInfo.JoinTime.ToString("yyyy-MM-dd");
                YouthDate.Text = soldierInfo.YouthTime.Year == 1 ? "--" : soldierInfo.YouthTime.Year == 9999 ? "--" : soldierInfo.YouthTime.ToString("yyyy-MM-dd");
                PartyDate.Text = soldierInfo.PartyTime.Year == 1 ? "--" : soldierInfo.PartyTime.Year == 9999 ? "--" : soldierInfo.PartyTime.ToString("yyyy-MM-dd");
                RankName.Text = CodeItem.GetCodeItem(soldierInfo.Rank).ItemName;
                DutyName.Text = CodeItem.GetCodeItem(soldierInfo.Duty).ItemName;
                Speciality1Name.Text = CodeItem.GetCodeItem(soldierInfo.Speciality1).ItemName;
                Speciality2Name.Text = soldierInfo.Speciality2;     // CodeItem.GetCodeItem(soldierInfo.Speciality2).ItemName;
                JoinPlace.Text = soldierInfo.JoinPlace;
                ConfirmID.Text = soldierInfo.ConfirmID;
                ChooseID.Text = soldierInfo.ChooseID;
                RegisterID.Text = soldierInfo.RegisterID;
                Height.Text = soldierInfo.Height == 0 ? "--" : soldierInfo.Height.ToString() + "cm";
                Weight.Text = soldierInfo.Weight == 0.0m ? "--" : soldierInfo.Weight.ToString() + "kg";
                SetUserName.Text = soldierInfo.SetUserName;
                SetTime.Text = soldierInfo.SetTime.ToString("yyyy-MM-dd");
                HomePlace.Text = soldierInfo.HomePlace;

                Hobby.Text = CodeItem.GetCodeItem(soldierInfo.Hobby) == null ? "" : CodeItem.GetCodeItem(soldierInfo.Hobby).ItemName;
                Political.Text = CodeItem.GetCodeItem(soldierInfo.Political) == null ? "" : CodeItem.GetCodeItem(soldierInfo.Political).ItemName;

                //新二
                //SGRegNO.Text = soldierInfo.SGRegNO;
                //SGGegDate.Text = soldierInfo.SGGegDate.Year == 9999 ? "" : soldierInfo.SGGegDate.ToString("yyyy-MM-dd");
                //Training.Text = soldierInfo.Training;
                //Reward.Text = soldierInfo.Reward;
                //Resume.Text =soldierInfo.Resume;
                //Remark.Text = soldierInfo.Remark;

                //NewDrillScoreDataContext ndsdc = new NewDrillScoreDataContext();
                //NewDrillScore nds = ndsdc.NewDrillScore.Where(o => o.SoldierId.Value == new Guid(soldierInfo.SoldierID)).FirstOrDefault();
                //if (nds != null)
                //{
                //    PoliticalEducation.Text = nds.PoliticalEducation;
                //    OPerationTheory.Text = nds.OPerationTheory;
                //    Queue.Text = nds.Queue;
                //    Skill.Text = nds.Skill;
                //    Ordinance.Text = nds.Ordinance;
                //    Stamina.Text = nds.Stamina;
                //}
                //if (soldierInfo.IsDelete)
                //{
                //    btnRestore.Visible = true;
                //    lblDelete.Visible = true;
                //}
                //if (soldierInfo.IsDelete)
                //{
                //    btnRestore.Visible = true;
                //    lblDelete.Visible = true;
                //}
                //BindTransferList();


            }
            else
            {
                lblTitle.Text = "您无权查看此兵员信息。";
                imgPhoto.ImageUrl = "~/Images/NoPhoto.png";
            }
        }
    }
    protected void btnRestore_Click(object sender, EventArgs e)
    {

        //TownSoldiersInfo soldierInfo = TownSoldiers.GetTownSoldiers(new Guid(Request["ID"].ToString()));
        //if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203001") ||
        //    Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101") ||
        //    ((Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400102") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203008")) && soldierInfo.DepartmentID.Substring(0, 4) == Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4)) ||
        //    (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203009") && soldierInfo.DepartmentID == Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID))
        //{
        //    if (soldier.Restore(soldierInfo.SoldierID, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName, DateTime.Now))
        //    {
        //        lblDelete.Text = "已恢复。";
        //        btnRestore.Visible = false;
        //    }
        //    else
        //        lblDelete.Text = "恢复失败。";
        //}
        //else
        //{
        //    lblDelete.Text = "权限不足。";
        //    btnRestore.Visible = false;
        //}

    }
    protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (gvTransfer.PageIndex * gvTransfer.PageSize + e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

        }
    }

    private void BindTransferList()
    {
        //gvTransfer.DataSource = TransferSoldier.GetTransferListBySoldierID(Request["ID"]);
        //gvTransfer.DataBind();
    }

    private void BindArichives()
    {
        DataView dv = SplitPage.ExecuteSQL("Select cast((sum(case when FirstScoreHg=2 then 1 else 0 end)) as decimal(18, 2))/ cast((Select Count(*) FROM Job_Score where sid='" + Request["ID"] + "' and year(StartTime)=##t0.[year] and Month(StartTime)=##t0.[Month]) as decimal(18, 2))  Score ,(cast([year] as varchar)+'年'+cast([Month] as varchar)+'月' ) Date From ( SELECT     Lid, YEAR(StartTime) AS 'Year', MONTH(StartTime) AS 'Month',FirstScoreHg  FROM         Job_Score WHERE     (Sid = '" + Request["ID"] + "' )) ##t0 group by [year],[Month]").Tables[0].DefaultView;
        this.ArchivesGrid.DataSource = dv;
        this.ArchivesGrid.DataBind();
    }

    public string GetDate(object obj, bool bl)
    {
        if (obj == null)
            return "";
        string[] strArry = obj.ToString().Split(new string[] { "年" }, StringSplitOptions.RemoveEmptyEntries);
        if (strArry == null || strArry.Length < 1)
            return "";
        if (bl)
        {
            return strArry[0].ToString();
        }
        else
        {
            return strArry[1].Replace("月", "");
        }
    }
    protected void ArchivesGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

        }
    }
}
